home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / dist / stor-layout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-17  |  31.0 KB  |  1,067 lines

  1. /* C-compiler utilities for types and variables storage layout
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include <stdio.h>
  23.  
  24. #include "tree.h"
  25. #include "rtl.h"   /* For GET_MODE_SIZE */
  26.  
  27. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  28. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  29. #define CEIL(x,y) (((x) + (y) - 1) / (y))
  30.  
  31. /* Data type for the expressions representing sizes of data types.
  32.    It is the first integer type laid out.
  33.    In C, this is int.  */
  34.  
  35. tree sizetype;
  36.  
  37. /* An integer constant with value 0 whose type is sizetype.  */
  38.  
  39. tree size_zero_node;
  40.  
  41. /* An integer constant with value 1 whose type is sizetype.  */
  42.  
  43. tree size_one_node;
  44.  
  45. #define GET_MODE_ALIGNMENT(MODE)   \
  46.   MIN (BIGGEST_ALIGNMENT,        \
  47.        MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
  48.  
  49. /* Chain of all permanent types we have allocated since last
  50.    call to get_permanent_types.  */
  51.  
  52. tree permanent_type_chain;
  53.  
  54. /* Chain of all temporary types we have allocated in this function.  */
  55.  
  56. tree temporary_type_chain;
  57.  
  58. /* When the chains is not null, these point at the last
  59.    types on the two chains.  These help us tell whether a type
  60.    is already on a chain.  */
  61. tree permanent_type_end;
  62. tree temporary_type_end;
  63.  
  64. /* Put the newly-made type T
  65.    on either permanent_type_chain or temporary_type_chain.
  66.    Types that are const or volatile variants of other types
  67.    are not put on any chain, since in the gdb symbol segment
  68.    we do not make those distinctions.
  69.  
  70.    If T is already on the chain, we do nothing.  */
  71.  
  72. void
  73. chain_type (t)
  74.      tree t;
  75. {
  76.   if (TYPE_MAIN_VARIANT (t) != t)
  77.     return;
  78.   if (TREE_CHAIN (t) != 0)
  79.     return;
  80.   if (TREE_PERMANENT (t))
  81.     {
  82.       /* If T is on the chain at the end, don't chain it to itself!  */
  83.       if (t == permanent_type_end)
  84.     return;
  85.       /* Add T to the end of the chain.  */
  86.       if (permanent_type_chain == 0)
  87.     permanent_type_chain = t;
  88.       else
  89.     TREE_CHAIN (permanent_type_end) = t;
  90.       permanent_type_end = t;
  91.     }
  92.   else
  93.     {
  94.       if (t == temporary_type_end)
  95.     return;
  96.       if (temporary_type_chain == 0)
  97.     temporary_type_chain = t;
  98.       else
  99.     TREE_CHAIN (temporary_type_end) = t;
  100.       temporary_type_end = t;
  101.     }
  102. }
  103.  
  104. /* Get a chain of all permanent types made since this function
  105.    was last called.  */
  106.  
  107. tree
  108. get_permanent_types ()
  109. {
  110.   register tree tem = permanent_type_chain;
  111.   permanent_type_chain = 0;
  112.   permanent_type_end = 0;
  113.   return tem;
  114. }
  115.  
  116. /* Get a chain of all temporary types made since this function
  117.    was last called.  */
  118.  
  119. tree
  120. get_temporary_types ()
  121. {
  122.   register tree tem = temporary_type_chain;
  123.   temporary_type_chain = 0;
  124.   temporary_type_end = 0;
  125.   return tem;
  126. }
  127.  
  128. /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded.  */
  129.  
  130. static tree pending_sizes;
  131.  
  132. /* Nonzero means cannot safely call expand_expr now,
  133.    so put variable sizes onto `pending_sizes' instead.  */
  134.  
  135. int immediate_size_expand;
  136.  
  137. tree
  138. get_pending_sizes ()
  139. {
  140.   tree chain = pending_sizes;
  141.   pending_sizes = 0;
  142.   return chain;
  143. }
  144.  
  145. /* Given a size SIZE that isn't constant, return a SAVE_EXPR
  146.    to serve as the actual size-expression for a type or decl.  */
  147.  
  148. static tree
  149. variable_size (size)
  150.      tree size;
  151. {
  152.   size = save_expr (size);
  153.  
  154.   if (global_bindings_p ())
  155.     {
  156.       error ("variable-size type declared outside of any function");
  157.       return build_int (1);
  158.     }
  159.  
  160.   if (immediate_size_expand)
  161.     expand_expr (size, 0, VOIDmode, 0);
  162.   else
  163.     pending_sizes = tree_cons (0, size, pending_sizes);
  164.  
  165.   return size;
  166. }
  167.  
  168. /* Return the machine mode to use for an aggregate of SIZE bits.
  169.  
  170.    Note!!!  We only use a non-BLKmode mode if the size matches exactly.
  171.    There used to be the idea of using DImode for anything whose
  172.    size was less than DImode but more than SImode.  This does not work
  173.    because DImode moves cannot be used to store such objects in memory.  */
  174.  
  175. #ifndef MAX_FIXED_MODE_SIZE
  176. #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
  177. #endif
  178.  
  179. static
  180. enum machine_mode
  181. agg_mode (size)
  182.      unsigned int size;
  183. {
  184.   register int units = size / BITS_PER_UNIT;
  185.   register enum machine_mode t, val;
  186.  
  187.   if (size % BITS_PER_UNIT != 0)
  188.     return BLKmode;
  189.  
  190.   if (size > MAX_FIXED_MODE_SIZE)
  191.     return BLKmode;
  192.  
  193.   /* Get the last mode which has this size.  */
  194.   val = BLKmode;
  195.   for (t = QImode; GET_MODE_CLASS (t) == MODE_INT;
  196.        t = (enum machine_mode) ((int) t + 1))
  197.     if (GET_MODE_SIZE (t) == units)
  198.       val = t;
  199.  
  200.   return val;
  201. }
  202.  
  203. /* Return an INTEGER_CST with value V and type from `sizetype'.  */
  204.  
  205. tree
  206. build_int (v)
  207.      int v;
  208. {
  209.   register tree t;
  210.   /* Type-size nodes already made for small sizes.  */
  211.   static tree size_table[33];
  212.  
  213.   if (v < 33 && size_table[v] != 0)
  214.     return size_table[v];
  215.   if (v < 33)
  216.     {
  217.       int temp = allocation_temporary_p ();
  218.       /* Make this a permanent node.  */
  219.       if (temp)
  220.     end_temporary_allocation ();
  221.       t = build_int_2 (v, 0);
  222.       TREE_TYPE (t) = sizetype;
  223.       size_table[v] = t;
  224.       if (temp)
  225.     resume_temporary_allocation ();
  226.     }
  227.   else
  228.     {
  229.       t = build_int_2 (v, 0);
  230.       TREE_TYPE (t) = sizetype;
  231.     }
  232.   return t;
  233. }
  234.  
  235. /* Combine operands OP1 and OP2 with arithmetic operation OPC.
  236.    OPC is a tree code.  Data type is taken from `sizetype',
  237.    If the operands are constant, so is the result.  */
  238.  
  239. tree
  240. genop (opc, op1, op2)
  241.      enum tree_code opc;
  242.      tree op1, op2;
  243. {
  244.   /* Handle the special case of two integer constants faster.  */
  245.   if (TREE_CODE (op1) == INTEGER_CST && TREE_CODE (op2) == INTEGER_CST)
  246.     {
  247.       /* And some specific cases even faster than that.  */
  248.       if (opc == PLUS_EXPR
  249.       && TREE_INT_CST_LOW (op1) == 0
  250.       && TREE_INT_CST_HIGH (op1) == 0)
  251.     return op2;
  252.       if (opc == MINUS_EXPR
  253.       && TREE_INT_CST_LOW (op2) == 0
  254.       && TREE_INT_CST_HIGH (op2) == 0)
  255.     return op1;
  256.       if (opc == MULT_EXPR
  257.       && TREE_INT_CST_LOW (op1) == 1
  258.       && TREE_INT_CST_HIGH (op1) == 0)
  259.     return op2;
  260.       if (opc == CEIL_DIV_EXPR
  261.       && TREE_INT_CST_LOW (op1) == TREE_INT_CST_LOW (op2)
  262.       && TREE_INT_CST_HIGH (op1) == TREE_INT_CST_HIGH (op2))
  263.     return size_one_node;
  264.       /* Handle general case of two integer constants.  */
  265.       return combine (opc, op1, op2);
  266.     }
  267.  
  268.   if (op1 == error_mark_node || op2 == error_mark_node)
  269.     return error_mark_node;
  270.  
  271.   return fold (build (opc, sizetype, op1, op2));
  272. }
  273.  
  274. /* Convert a size which is SIZE when expressed in unit INUNITS
  275.    into the units OUTUNITS.  Rounds up if conversion is not exact.
  276.    If SIZE is constant, so is the result.  */
  277.  
  278. tree
  279. convert_units (size, inunits, outunits)
  280.    tree size;
  281.    register int inunits, outunits;
  282. {
  283.   register tree t;
  284.  
  285.   if (inunits == outunits)
  286.     return size;
  287.   /* Check for inunits divisible by outunits.
  288.      In that case, just multiply by their ratio.  */
  289.   if (0 == (inunits % outunits))
  290.     return genop (MULT_EXPR, size, build_int (inunits / outunits));
  291.   /* The inverse case.  */
  292.   if (0 == (outunits % inunits))
  293.     {
  294.       /* Discard anything in SIZE to round it up to a multiple
  295.      of a number N that divides our current divisor.  */
  296.       if (TREE_CODE (size) == MULT_EXPR
  297.       && TREE_CODE (TREE_OPERAND (size, 1)) == INTEGER_CST
  298.       && 0 == (outunits / inunits) % TREE_INT_CST_LOW (TREE_OPERAND (size, 1))
  299.       && TREE_CODE (TREE_OPERAND (size, 0)) == CEIL_DIV_EXPR
  300.       && tree_int_cst_equal (TREE_OPERAND (size, 1),
  301.                  TREE_OPERAND (TREE_OPERAND (size, 0), 1)))
  302.     size = TREE_OPERAND (TREE_OPERAND (size, 0), 0);
  303.       return genop (CEIL_DIV_EXPR, size, build_int (outunits / inunits));
  304.     }
  305.   /* The general case.  */
  306.   t = genop (MULT_EXPR, size,
  307.          build_int (inunits)); /* convert to bits */
  308.   return genop (CEIL_DIV_EXPR, t,
  309.         build_int (outunits)); /* then to outunits */
  310. }
  311.  
  312. /* Set the size, mode and alignment of a ..._DECL node.
  313.    TYPE_DECL does need this for C++.  It is up to language-specific
  314.    code to intialize the DECL_OFFSET of TYPE_DECL nodes.
  315.    Note that LABEL_DECL and CONST_DECL nodes do not need this,
  316.    and FUNCTION_DECL nodes have them set up in a special (and simple) way.
  317.    Don't call layout_decl for them.
  318.  
  319.    KNOWN_ALIGN is the amount of alignment we can assume this
  320.    decl has with no special effort.  It is relevant only for FIELD_DECLs
  321.    and depends on the previous fields.
  322.    All that matters about KNOWN_ALIGN is which powers of 2 divide it.
  323.    If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
  324.    the record will be aligned to suit.  */
  325.  
  326. void
  327. layout_decl (decl, known_align)
  328.      tree decl;
  329.      unsigned known_align;
  330. {
  331.   register tree type = TREE_TYPE (decl);
  332.   register enum tree_code code = TREE_CODE (decl);
  333.   int spec_size = DECL_SIZE_UNIT (decl);
  334.   int bitsize;
  335.  
  336.   if (code == CONST_DECL)
  337.     return;
  338.  
  339.   if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
  340.       && code != FIELD_DECL && code != TYPE_DECL)
  341.     abort ();
  342.  
  343.   if (type == error_mark_node)
  344.     {
  345.       type = void_type_node;
  346.       spec_size = 0;
  347.     }
  348.   if (TYPE_SIZE_UNIT (type) == 0)
  349.     abort ();
  350.  
  351.   /* Usually the size and mode come from the data type without change.  */
  352.  
  353.   DECL_MODE (decl) = TYPE_MODE (type);
  354.   DECL_SIZE (decl) = TYPE_SIZE (type);
  355.   DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
  356.   TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
  357.  
  358.   if (code == FIELD_DECL && TREE_PACKED (decl))
  359.     {
  360.       /* This is a bit-field.  We don't know how to handle
  361.      them except for integers and enums, and front end should
  362.      never generate them otherwise.  */
  363.  
  364.       if (! (TREE_CODE (type) == INTEGER_TYPE
  365.          || TREE_CODE (type) == ENUMERAL_TYPE))
  366.     abort ();
  367.  
  368.       if (spec_size == 0)
  369.     abort ();
  370.  
  371.       /* Mode is "integer bit field".  */
  372.       DECL_MODE (decl) = BImode;
  373.       /* Size is specified number of bits.  */
  374.       DECL_SIZE (decl) = size_one_node;
  375.       DECL_SIZE_UNIT (decl) = spec_size;
  376.     }
  377.   /* Force alignment required for the data type.
  378.      But if the decl itself wants greater alignment, don't override that.  */
  379.   else if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
  380.     DECL_ALIGN (decl) = TYPE_ALIGN (type);
  381.  
  382.   if (DECL_SIZE (decl))
  383.     bitsize = TREE_INT_CST_LOW (DECL_SIZE (decl)) * DECL_SIZE_UNIT (decl);
  384.  
  385.   /* See if we can use a scalar mode such as QImode or SImode
  386.      in place of BLKmode or a packed byte mode.  */
  387.   /* Conditions are: a fixed size that is correct for another mode
  388.      and occupying a complete byte or bytes on proper boundary.  */
  389.   if ((DECL_MODE (decl) == BLKmode
  390.        || DECL_MODE (decl) == BImode)
  391.       /* Don't do this if DECL's type requires it to be BLKmode.  */
  392.       && TYPE_MODE (type) != BLKmode
  393.       && TYPE_SIZE (type) != 0
  394.       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  395.     {
  396.       register enum machine_mode xmode = agg_mode (bitsize);
  397.  
  398.       if (xmode != BLKmode
  399.       && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
  400.     {
  401.       DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
  402.                    DECL_ALIGN (decl));
  403.       DECL_MODE (decl) = xmode;
  404.       DECL_SIZE (decl) = build_int (GET_MODE_SIZE (xmode));
  405.       DECL_SIZE_UNIT (decl) = BITS_PER_UNIT;
  406.       bitsize = GET_MODE_BITSIZE (xmode);
  407.     }
  408.     }
  409.  
  410.   /* Don't let more than one word of an aggregate occupy one register,
  411.      since then the SUBREG used to access the high part would malfunction.
  412.      Check that the expected # of registers is big enough that they
  413.      seem to hold this variable with just a word per register.  */
  414.   if (DECL_SIZE (decl) != 0
  415.       && (TREE_CODE (type) == RECORD_TYPE
  416.       || TREE_CODE (type) == UNION_TYPE
  417.       || TREE_CODE (type) == ARRAY_TYPE))
  418.     {
  419.       /* This test is not exactly right, since we really want the minimum
  420.      number of regs in any class that can hold this mode.
  421.      But it does distinguish the machines we need to distinguish,
  422.      for now.  */
  423.       if (CLASS_MAX_NREGS (ALL_REGS, TYPE_MODE (type)) * BITS_PER_WORD
  424.       < bitsize)
  425.     TREE_ADDRESSABLE (decl) = 1;
  426.     }
  427.  
  428.   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
  429.   if (DECL_SIZE (decl) != 0 && ! TREE_LITERAL (DECL_SIZE (decl)))
  430.     DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
  431. }
  432.  
  433. /* Lay out a RECORD_TYPE type (a C struct).
  434.    This means laying out the fields, determining their offsets,
  435.    and computing the overall size and required alignment of the record.
  436.    Note that if you set the TYPE_ALIGN before calling this
  437.    then the struct is aligned to at least that boundary.
  438.  
  439.    If the type has basetypes, you must call layout_basetypes
  440.    before calling this function.  */
  441.  
  442. static void
  443. layout_record (rec)
  444.      tree rec;
  445. {
  446.   register tree field;
  447. #ifdef STRUCTURE_SIZE_BOUNDARY
  448.   int record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
  449. #else
  450.   int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
  451. #endif
  452.   /* These must be laid out *after* the record is.  */
  453.   tree pending_statics = NULL_TREE;
  454.   /* Record size so far is CONST_SIZE + VAR_SIZE * SIZE_UNIT bits,
  455.      where CONST_SIZE is an integer
  456.      and VAR_SIZE is a tree expression.
  457.      If VAR_SIZE is null, the size is just CONST_SIZE.
  458.      Naturally we try to avoid using VAR_SIZE.  */
  459.   register int const_size = 0;
  460.   register tree var_size = 0;
  461.   register int size_unit = BITS_PER_UNIT;
  462.  
  463. #if 0
  464.   /* If there are basetypes, the caller should already have
  465.      laid them out.  Leave space at the beginning for them.  */
  466.   if (TYPE_SIZE (rec) != 0)
  467.     {
  468.       if (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST)
  469.     const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
  470.       else
  471.     var_size = TYPE_SIZE (rec);
  472.       size_unit = TYPE_SIZE_UNIT (rec);
  473.     }
  474. #endif /* 0 */
  475.  
  476.   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  477.     {
  478.       register int desired_align;
  479.  
  480.       /* If FIELD is a VAR_DECL, then treat it like a separate variable,
  481.      not really like a structure field.
  482.      If it is a FUNCTION_DECL, it's a method.
  483.      In both cases, all we do is lay out the decl,
  484.      and we do it *after* the record is laid out.  */
  485.  
  486.       if (TREE_CODE (field) == VAR_DECL)
  487.     {
  488.       pending_statics = tree_cons (NULL, field, pending_statics);
  489.       continue;
  490.     }
  491.       /* Enumerators and enum types which are local to this class need not
  492.      be laid out.  */
  493.       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
  494.     continue;
  495.  
  496.       /* Lay out the field so we know what alignment it needs.
  497.      For KNOWN_ALIGN, pass the number of bits from start of record
  498.      or some divisor of it.  */
  499.  
  500.       layout_decl (field, var_size ? size_unit : const_size);
  501.       desired_align = DECL_ALIGN (field);
  502.  
  503.       /* Record must have at least as much alignment as any field.
  504.      Otherwise, the alignment of the field within the record
  505.      is meaningless.  */
  506.  
  507.       record_align = MAX (record_align, desired_align);
  508. #ifdef PCC_BITFIELD_TYPE_MATTERS
  509.       /* In PCC on Vax, Sony, etc., a bit field of declare type `int'
  510.      forces the entire structure to have `int' alignment.  */
  511.       if (DECL_NAME (field) != 0)
  512.     record_align = MAX (record_align, TYPE_ALIGN (TREE_TYPE (field)));
  513. #endif
  514.  
  515.       /* Does this field automatically have alignment it needs
  516.      by virtue of the fields that precede it and the record's
  517.      own alignment?  */
  518.  
  519.       if (const_size % desired_align != 0
  520.       || (size_unit % desired_align != 0
  521.           && var_size))
  522.     {
  523.       /* No, we need to skip space before this field.
  524.          Bump the cumulative size to multiple of field alignment.  */
  525.  
  526.       if (var_size == 0
  527.           || size_unit % desired_align == 0)
  528.         const_size
  529.           = CEIL (const_size, desired_align) * desired_align;
  530.       else
  531.         {
  532.           var_size
  533.         = genop (PLUS_EXPR, var_size,
  534.              build_int (CEIL (const_size, size_unit)));
  535.           const_size = 0;
  536.           var_size = convert_units (var_size, size_unit, desired_align);
  537.           size_unit = desired_align;
  538.         }
  539.     }
  540.  
  541. #ifdef PCC_BITFIELD_TYPE_MATTERS
  542.       if (TREE_CODE (field) == FIELD_DECL
  543.       && TREE_TYPE (field) != error_mark_node)
  544.     {
  545.       int type_align = TYPE_ALIGN (TREE_TYPE (field));
  546.       register tree dsize = DECL_SIZE (field);
  547.       int field_size = TREE_INT_CST_LOW (dsize) * DECL_SIZE_UNIT (field);
  548.  
  549.       /* A bit field may not span the unit of alignment of its type.
  550.          Advance to next boundary if necessary.  */
  551.       if (const_size / type_align
  552.           != (const_size + field_size - 1) / type_align)
  553.         const_size = CEIL (const_size, type_align) * type_align;
  554.     }
  555. #endif
  556.  
  557.       /* Size so far becomes the offset of this field.  */
  558.  
  559.       DECL_OFFSET (field) = const_size;
  560.       DECL_VOFFSET (field) = var_size;
  561.       DECL_VOFFSET_UNIT (field) = size_unit;
  562.  
  563.       /* If this field is an anonymous union,
  564.      give each union-member the same offset as the union has.  */
  565.  
  566.       if (DECL_NAME (field) == 0
  567.       && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
  568.     {
  569.       tree uelt = TYPE_FIELDS (TREE_TYPE (field));
  570.       for (; uelt; uelt = TREE_CHAIN (uelt))
  571.         {
  572.           DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
  573.           DECL_OFFSET (uelt) = DECL_OFFSET (field);
  574.           DECL_VOFFSET (uelt) = DECL_VOFFSET (field);
  575.           DECL_VOFFSET_UNIT (uelt) = DECL_VOFFSET_UNIT (field);
  576.         }
  577.     }
  578.  
  579.       /* Now add size of this field to the size of the record.  */
  580.  
  581.       {
  582.         register tree dsize = DECL_SIZE (field);
  583.  
  584.     if (TREE_LITERAL (dsize))
  585.       const_size += TREE_INT_CST_LOW (dsize) * DECL_SIZE_UNIT (field);
  586.     else if (var_size == 0)
  587.       {
  588.         var_size = dsize;
  589.         size_unit = DECL_SIZE_UNIT (field);
  590.       }
  591.     else
  592.       {
  593.         register int tunits = MIN (size_unit, DECL_SIZE_UNIT (field));
  594.         var_size
  595.           = genop (PLUS_EXPR,
  596.                convert_units (var_size, size_unit, tunits),
  597.                convert_units (dsize, DECL_SIZE_UNIT (field), tunits));
  598.       }
  599.       }
  600.     }
  601.  
  602.   /* Work out the total size and alignment of the record
  603.      as one expression and store in the record type.
  604.      Round it up to a multiple of the record's alignment.  */
  605.  
  606.   if (var_size == 0)
  607.     TYPE_SIZE (rec)
  608.       = build_int (CEIL (CEIL (const_size, record_align) * record_align,
  609.              size_unit));
  610.   else
  611.     {
  612.       if (const_size)
  613.     var_size
  614.       = genop (PLUS_EXPR, var_size,
  615.            build_int (CEIL (const_size, size_unit)));
  616.       TYPE_SIZE (rec)
  617.     = convert_units (var_size,
  618.              size_unit,
  619.              record_align);
  620.       size_unit = record_align;
  621.     }
  622.  
  623.   TYPE_SIZE (rec) = convert_units (TYPE_SIZE (rec), size_unit,
  624.                    BITS_PER_UNIT);
  625.   TYPE_SIZE_UNIT (rec) = BITS_PER_UNIT;
  626.   TYPE_ALIGN (rec) = MIN (BIGGEST_ALIGNMENT, record_align);
  627.  
  628.   /* Lay out any static members.  This is done now
  629.      because their type may use the record's type.  */
  630.  
  631.   for (field = pending_statics; field; field = TREE_CHAIN (field))
  632.     layout_decl (TREE_VALUE (field), 0);
  633. }
  634.  
  635. /* Lay out a UNION_TYPE type.
  636.    Lay out all the fields, set their offsets to zero,
  637.    and compute the size and alignment of the union (maximum of any field).
  638.    Note that if you set the TYPE_ALIGN before calling this
  639.    then the union align is aligned to at least that boundary.  */
  640.  
  641. static void
  642. layout_union (rec)
  643.      tree rec;
  644. {
  645.   register tree field;
  646. #ifdef STRUCTURE_SIZE_BOUNDARY
  647.   int union_align = STRUCTURE_SIZE_BOUNDARY;
  648. #else
  649.   int union_align = BITS_PER_UNIT;
  650. #endif
  651.  
  652.   /* The size of the union, based on the fields scanned so far,
  653.      is max (CONST_SIZE, VAR_SIZE).
  654.      VAR_SIZE may be null; then CONST_SIZE by itself is the size.  */
  655.   register int const_size = 0;
  656.   register tree var_size = 0;
  657.  
  658.   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  659.     {
  660. #if 0 /* This should be in a language-specific file
  661.      since it needs to use language-specific terminology.  */
  662.       if (TREE_STATIC (field))
  663.     {
  664.       error_with_decl (field, "field `%s' declared static in union");
  665.       TREE_STATIC (field) = 0;
  666.     }
  667. #endif
  668.  
  669.       /* Ignore enumerators and enum types local to the union.  */
  670.       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
  671.     continue;
  672.  
  673.       layout_decl (field, 0);
  674.       DECL_OFFSET (field) = 0;
  675.       DECL_VOFFSET (field) = 0;
  676.       DECL_VOFFSET_UNIT (field) = BITS_PER_UNIT;
  677.  
  678.       /* Union must be at least as aligned as any field requires.  */
  679.  
  680.       union_align = MAX (union_align, DECL_ALIGN (field));
  681.  
  682. #ifdef PCC_BITFIELD_TYPE_MATTERS
  683.       /* On the m88000, a bit field of declare type `int'
  684.      forces the entire union to have `int' alignment.  */
  685.       union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
  686. #endif
  687.  
  688.       /* Set union_size to max (decl_size, union_size).
  689.      There are more and less general ways to do this.
  690.      Use only CONST_SIZE unless forced to use VAR_SIZE.  */
  691.  
  692.       if (TREE_LITERAL (DECL_SIZE (field)))
  693.     const_size = MAX (const_size,
  694.               TREE_INT_CST_LOW (DECL_SIZE (field))
  695.               * DECL_SIZE_UNIT (field));
  696.       else if (var_size == 0)
  697.     var_size = convert_units (DECL_SIZE (field),
  698.                   DECL_SIZE_UNIT (field),
  699.                   BITS_PER_UNIT);
  700.       else
  701.     var_size = genop (MAX_EXPR,
  702.               convert_units (DECL_SIZE (field),
  703.                      DECL_SIZE_UNIT (field),
  704.                      BITS_PER_UNIT),
  705.               var_size);
  706.     }
  707.  
  708.   /* Determine the ultimate size of the union (in bytes).  */
  709.   if (NULL == var_size)
  710.     TYPE_SIZE (rec) = build_int (CEIL (const_size, BITS_PER_UNIT));
  711.   else if (const_size == 0)
  712.     TYPE_SIZE (rec) = var_size;
  713.   else
  714.     TYPE_SIZE (rec) = genop (MAX_EXPR, var_size,
  715.                  build_int (CEIL (const_size, BITS_PER_UNIT)));
  716.  
  717.   /* Determine the desired alignment.  */
  718.   union_align = MIN (BIGGEST_ALIGNMENT, union_align);
  719.   TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
  720.  
  721.   /* Round the size up to be a multiple of the required alignment */
  722.   TYPE_SIZE (rec)
  723.     = convert_units (TYPE_SIZE (rec), BITS_PER_UNIT, TYPE_ALIGN (rec));
  724.   TYPE_SIZE_UNIT (rec) = TYPE_ALIGN (rec);
  725. }
  726.  
  727. /* Calculate the mode, size, and alignment for TYPE.
  728.    For an array type, calculate the element separation as well.
  729.    Record TYPE on the chain of permanent or temporary types
  730.    so that dbxout will find out about it.
  731.  
  732.    TYPE_SIZE of a type is nonzero if the type has been laid out already.
  733.    layout_type does nothing on such a type.
  734.  
  735.    If the type is incomplete, its TYPE_SIZE remains zero.  */
  736.  
  737. void
  738. layout_type (type)
  739.      tree type;
  740. {
  741.   int old;
  742.   int temporary = 0;
  743.  
  744.   if (type == 0)
  745.     abort ();
  746.  
  747.   /* Do nothing if type has been laid out before.  */
  748.   if (TYPE_SIZE (type))
  749.     return;
  750.  
  751.   /* Make sure all nodes we allocate are not momentary;
  752.      they must last past the current statement.  */
  753.   old  = suspend_momentary ();
  754.   if (TREE_PERMANENT (type) && allocation_temporary_p ())
  755.     {
  756.       temporary = 1;
  757.       end_temporary_allocation ();
  758.     }
  759.  
  760.   chain_type (type);
  761.  
  762.   switch (TREE_CODE (type))
  763.     {
  764.     case LANG_TYPE:
  765.       /* This kind of type is the responsibility
  766.      of the languge-specific code.  */
  767.       abort ();
  768.  
  769.     case VOID_TYPE:
  770.       TYPE_SIZE (type) = size_zero_node;
  771.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  772.       TYPE_ALIGN (type) = 1;
  773.       TYPE_MODE (type) = VOIDmode;
  774.       break;
  775.  
  776.     case INTEGER_TYPE:
  777.     case ENUMERAL_TYPE:
  778.       if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) >= 0)
  779.     TREE_UNSIGNED (type) = 1;
  780.  
  781.       /* What follows is like agg_mode except that it ignores
  782.      MAX_FIXED_MODE_SIZE.  That applies only to structures.  */
  783.       {
  784.     enum machine_mode mode, t;
  785.  
  786.     /* Get the last mode which has this size.  */
  787.     mode = BLKmode;
  788.     for (t = QImode; GET_MODE_CLASS (t) == MODE_INT;
  789.          t = (enum machine_mode) ((int) t + 1))
  790.       if (GET_MODE_BITSIZE (t) == TYPE_PRECISION (type))
  791.         mode = t;
  792.  
  793.     TYPE_MODE (type) = mode;
  794.       }
  795.       TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
  796.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  797.       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
  798.       break;
  799.  
  800.     case REAL_TYPE:
  801.       {
  802.     register int prec = TYPE_PRECISION (type);
  803.     if (prec <= GET_MODE_BITSIZE (SFmode))
  804.       TYPE_MODE (type) = SFmode;
  805.     else if (prec <= GET_MODE_BITSIZE (DFmode))
  806.       TYPE_MODE (type) = DFmode;
  807.     else if (prec <= GET_MODE_BITSIZE (TFmode))
  808.       TYPE_MODE (type) = TFmode;
  809.     else
  810.       abort ();
  811.       }
  812.       TYPE_SIZE (type) = build_int (GET_MODE_SIZE (TYPE_MODE (type)));
  813.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  814.       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
  815.       break;
  816.  
  817.     case POINTER_TYPE:
  818.     case REFERENCE_TYPE:
  819.       TYPE_MODE (type) = Pmode;
  820.       TYPE_SIZE (type) = build_int (POINTER_SIZE / BITS_PER_UNIT);
  821.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  822.       TYPE_ALIGN (type) = POINTER_BOUNDARY;
  823.       TREE_UNSIGNED (type) = 1;
  824.       TYPE_PRECISION (type) = POINTER_SIZE;
  825.       break;
  826.  
  827.     case ARRAY_TYPE:
  828.       {
  829.     register tree index = TYPE_DOMAIN (type);
  830.     register tree length;
  831.     register tree element = TREE_TYPE (type);
  832.  
  833. /*     layout_type (element);  */
  834.     build_pointer_type (element);
  835.  
  836.     if (index == 0)
  837.       length = 0;
  838.     else
  839.       length = genop (PLUS_EXPR, size_one_node,
  840.               genop (MINUS_EXPR, TYPE_MAX_VALUE (index),
  841.                  TYPE_MIN_VALUE (index)));
  842.  
  843.     if (TREE_PACKED (type))
  844.       abort ();  /* ??? Not written yet since not needed for C.  */
  845.  
  846.     TYPE_SIZE_UNIT (type) = TYPE_SIZE_UNIT (element);
  847.     if (length && TYPE_SIZE (element))
  848.       TYPE_SIZE (type) = genop (MULT_EXPR, TYPE_SIZE (element), length);
  849.     TYPE_SEP (type) = TYPE_SIZE (element);
  850.     TYPE_SEP_UNIT (type) = TYPE_SIZE_UNIT (element);
  851.     TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
  852.     TYPE_MODE (type) = BLKmode;
  853.     if (TYPE_SIZE (type) != 0
  854.         && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  855.         /* BLKmode elements force BLKmode aggregate;
  856.            else extract/store fields may lose.  */
  857.         && TYPE_MODE (TREE_TYPE (type)) != BLKmode
  858. #ifdef STRICT_ALIGNMENT
  859.         && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  860.         || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  861.                      * TYPE_SIZE_UNIT (type)))
  862. #endif
  863.         )
  864.       {
  865.         TYPE_MODE (type)
  866.           = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  867.               * TYPE_SIZE_UNIT (type));
  868.       }
  869.     break;
  870.       }
  871.  
  872.     case RECORD_TYPE:
  873.       layout_record (type);
  874.       TYPE_MODE (type) = BLKmode;
  875.       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  876.       /* If structure's known alignment is less than
  877.          what the scalar mode would need, and it matters,
  878.          then stick with BLKmode.  */
  879. #ifdef STRICT_ALIGNMENT
  880.       && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  881.           || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  882.                        * TYPE_SIZE_UNIT (type)))
  883. #endif
  884.       )
  885.     {
  886.       tree field;
  887.       /* A record which has any BLKmode members must itself be BLKmode;
  888.          it can't go in a register.  */
  889.       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  890.         {
  891.           if (TYPE_MODE (TREE_TYPE (field)) == BLKmode)
  892.         goto record_lose;
  893.  
  894.           /* Must be BLKmode if any field crosses a word boundary,
  895.          since extract_bit_field can't handle that in registers.  */
  896.           if (DECL_OFFSET (field) / BITS_PER_WORD
  897.           != ((TREE_INT_CST_LOW (DECL_SIZE (field)) * DECL_SIZE_UNIT (field)
  898.                + DECL_OFFSET (field) - 1)
  899.               / BITS_PER_WORD))
  900.         goto record_lose;
  901.         }
  902.       
  903.       TYPE_MODE (type)
  904.         = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  905.             * TYPE_SIZE_UNIT (type));
  906.     record_lose: ;
  907.     }
  908.       break;
  909.  
  910.     case UNION_TYPE:
  911.       layout_union (type);
  912.       TYPE_MODE (type) = BLKmode;
  913.       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  914.       /* If structure's known alignment is less than
  915.          what the scalar mode would need, and it matters,
  916.          then stick with BLKmode.  */
  917. #ifdef STRICT_ALIGNMENT
  918.       && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  919.           || TYPE_ALIGN (type) >= (TREE_INT_CST_LOW (TYPE_SIZE (type))
  920.                        * TYPE_SIZE_UNIT (type)))
  921. #endif
  922.       )
  923.     {
  924.       tree field;
  925.       /* A union which has any BLKmode members must itself be BLKmode;
  926.          it can't go in a register.  */
  927.       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  928.         if (TYPE_MODE (TREE_TYPE (field)) == BLKmode)
  929.           goto union_lose;
  930.  
  931.       TYPE_MODE (type)
  932.         = agg_mode (TREE_INT_CST_LOW (TYPE_SIZE (type))
  933.             * TYPE_SIZE_UNIT (type));
  934.     union_lose: ;
  935.     }
  936.       break;
  937.  
  938.     case FUNCTION_TYPE:
  939.     case METHOD_TYPE:
  940.       TYPE_MODE (type) = EPmode;
  941.       TYPE_SIZE (type) = build_int (2 * POINTER_SIZE / BITS_PER_UNIT);
  942.       TYPE_SIZE_UNIT (type) = BITS_PER_UNIT;
  943.       TYPE_ALIGN (type) = POINTER_BOUNDARY;
  944.       break;
  945.  
  946.     default:
  947.       abort ();
  948.     } /* end switch */
  949.  
  950.   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
  951.   if (TYPE_SIZE (type) != 0 && ! TREE_LITERAL (TYPE_SIZE (type)))
  952.     TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
  953.  
  954.   /* Also layout any other variants of the type.  */
  955.   if (TYPE_NEXT_VARIANT (type)
  956.       || type != TYPE_MAIN_VARIANT (type))
  957.     {
  958.       tree variant;
  959.       /* Record layout info of this variant.  */
  960.       tree size = TYPE_SIZE (type);
  961.       int size_unit = TYPE_SIZE_UNIT (type);
  962.       int align = TYPE_ALIGN (type);
  963.       enum machine_mode mode = TYPE_MODE (type);
  964.  
  965.       /* Copy it into all variants.  */
  966.       for (variant = TYPE_MAIN_VARIANT (type);
  967.        variant;
  968.        variant = TYPE_NEXT_VARIANT (variant))
  969.     {
  970.       TYPE_SIZE (variant) = size;
  971.       TYPE_SIZE_UNIT (variant) = size_unit;
  972.       TYPE_ALIGN (variant) = align;
  973.       TYPE_MODE (variant) = mode;
  974.     }
  975.     }
  976.     
  977.   if (temporary)
  978.     resume_temporary_allocation ();
  979.   resume_momentary (old);
  980. }
  981.  
  982. /* Create and return a type for signed integers of PRECISION bits.  */
  983.  
  984. tree
  985. make_signed_type (precision)
  986.      int precision;
  987. {
  988.   register tree type = make_node (INTEGER_TYPE);
  989.  
  990.   TYPE_PRECISION (type) = precision;
  991.  
  992.   /* Create the extreme values based on the number of bits.  */
  993.  
  994.   TYPE_MIN_VALUE (type)
  995.     = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? 0 : (-1)<<(precision-1)),
  996.            (-1)<<(precision-HOST_BITS_PER_INT-1 > 0
  997.               ? precision-HOST_BITS_PER_INT-1
  998.               : 0));
  999.   TYPE_MAX_VALUE (type)
  1000.     = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? -1 : (1<<(precision-1))-1),
  1001.            (precision-HOST_BITS_PER_INT-1 > 0
  1002.             ? (1<<(precision-HOST_BITS_PER_INT-1))-1
  1003.             : 0));
  1004.  
  1005.   /* Give this type's extreme values this type as their type.  */
  1006.  
  1007.   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  1008.   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  1009.  
  1010.   /* The first type made with this or `make_unsigned_type'
  1011.      is the type for size values.  */
  1012.  
  1013.   if (sizetype == 0)
  1014.     sizetype = type;
  1015.  
  1016.   /* Lay out the type: set its alignment, size, etc.  */
  1017.  
  1018.   layout_type (type);
  1019.  
  1020.   return type;
  1021. }
  1022.  
  1023. /* Create and return a type for unsigned integers of PRECISION bits.  */
  1024.  
  1025. tree
  1026. make_unsigned_type (precision)
  1027.      int precision;
  1028. {
  1029.   register tree type = make_node (INTEGER_TYPE);
  1030.  
  1031.   TYPE_PRECISION (type) = precision;
  1032.  
  1033.   /* The first type made with this or `make_unsigned_type'
  1034.      is the type for size values.  */
  1035.  
  1036.   if (sizetype == 0)
  1037.     sizetype = type;
  1038.  
  1039.   fixup_unsigned_type (type);
  1040.   return type;
  1041. }
  1042.  
  1043. /* Set the extreme values of TYPE based on its precision in bits,
  1044.    the lay it out.  This is used both in `make_unsigned_type'
  1045.    and for enumeral types.  */
  1046.  
  1047. void
  1048. fixup_unsigned_type (type)
  1049.      tree type;
  1050. {
  1051.   register int precision = TYPE_PRECISION (type);
  1052.  
  1053.   TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
  1054.   TYPE_MAX_VALUE (type)
  1055.     = build_int_2 (precision-HOST_BITS_PER_INT >= 0 ? -1 : (1<<precision)-1,
  1056.            precision-HOST_BITS_PER_INT > 0
  1057.            ? ((unsigned) ~0
  1058.               >> (HOST_BITS_PER_INT - (precision - HOST_BITS_PER_INT)))
  1059.            : 0);
  1060.   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  1061.   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  1062.  
  1063.   /* Lay out the type: set its alignment, size, etc.  */
  1064.  
  1065.   layout_type (type);
  1066. }
  1067.